#include <iostream>
#include <complex>
#include <cmath>
#include <vector>

#define MAX_ERROR 0.01
const std::complex<double> J(0, 1);
//

#define A2P(angle) (3.1415926 * (angle) / 180)
int main()
{

    // 固定住 ZS ES n
    // 目标量 E0 Z0 fa
    // 扫描量 ZL EL
    double ZS = 50.01;
    double ES = 92.42;
    double ZLLEFT = 0.01;
    double ZLRIGHT = 75;
    double ELLEFT = 0.01;
    double ELRIGHT = 72;

    double n = 0.39;
    double fa = 2.5;
    double f0 = 2;

    double Z0 = 70.71;
    double E0 = 90;
    double ELTmpFa = 0;
    double ESTmpFa = 0;
    double ELTmpFd = 0;
    double ESTmpFd = 0;
    double ELTmpF0 = 0;
    double ESTmpF0 = 0;
    std::complex<double> Aa;
    std::complex<double> A0;
    std::complex<double> B0;
    std::complex<double> C0;
    int FinalFlag = 0;
    double FlagDis = 0;
    double FlagE0 = 0;
    double FlagZ0 = 0;

    double FineEL = 0;
    double FineZL = 0;

    double FineELInTry = 0;
    double FineZLInTry = 0;
    double LargestDistance = 99999;
    double LargestDistanceTry = 99999;
    // 扫描 EL ES 固定ZS
    for (double EL = ELLEFT; EL <= ELRIGHT; EL += 0.01)
    {

        for (double ZL = ZLLEFT; ZL <= ZLRIGHT; ES += 0.01)
        {
            ELTmpFa = A2P(EL) * fa / f0;
            ESTmpFa = A2P(ES) * fa / f0;
            ELTmpF0 = A2P(EL);
            ESTmpF0 = A2P(ES);
            A0 = cos(ESTmpF0) * cos(ESTmpF0) + (sin(ESTmpF0) * (ZL * cos(ESTmpF0) * tan(ELTmpF0) * n * n * J + ZS * sin(ESTmpF0) * J) * J) / ZS;
            B0 = cos(ESTmpF0) * (ZL * cos(ESTmpF0) * tan(ELTmpF0) * n * n * J + ZS * sin(ESTmpF0) * J) + ZS * cos(ESTmpF0) * sin(ESTmpF0) * J;
            C0 = (cos(ESTmpF0) * sin(ESTmpF0) * J) / ZS + (sin(ESTmpF0) * (cos(ESTmpF0) - (ZL * n * n * sin(ESTmpF0) * tan(ELTmpF0)) / ZS) * J) / ZS;
            double Z0tmp = abs(sqrt(B0 / C0));
            double E0tmp = abs(acos(A0));
            Aa = cos(ESTmpFa) * cos(ESTmpFa) + (sin(ESTmpFa) * (ZL * cos(ESTmpFa) * tan(ELTmpFa) * n * n * J + ZS * sin(ESTmpFa) * J) * J) / ZS;
            FlagDis = abs(1.0 + Aa);
            FlagE0 = abs(E0tmp - E0);
            FlagZ0 = abs(Z0tmp - Z0);
            if ((FlagDis < MAX_ERROR) && (FlagE0 < MAX_ERROR) && (FlagZ0 < MAX_ERROR))
            {
                FinalFlag = 1;
                // 综合距离最小的解
                if (FlagDis + FlagE0 + FlagZ0 < LargestDistance)
                {
                    LargestDistance = FlagDis + FlagE0 + FlagZ0;
                    FineZL = ZL;
                    FineEL = EL;
                }
            }
            else
            {
                if (FlagDis + FlagE0 + FlagZ0 < LargestDistanceTry)
                {
                    FineELInTry = EL;
                    FineZLInTry = ZL;
                    LargestDistanceTry = FlagDis + FlagE0 + FlagZ0;
                }
            }
        }
    }
    if (FinalFlag != 0)
    {
        printf("综合最好解: ZS = %lf, ZL = %lf, EL = %lf 差距: %lf\n", ZS, FineZL, FineEL, LargestDistance);
    }
    else
    {
        printf("最收敛尝试: ZS = %lf, ZL = %lf, EL = %lf 差距: %lf\n", ZS, FineZLInTry, FineELInTry, LargestDistanceTry);
    }
    return 0;
}

// EL修改求解
/*
#include <iostream>
#include <complex>
#include <cmath>
#include <vector>

#define MAX_ERROR 0.01
const std::complex<double> J(0, 1);
//

#define A2P(angle) (3.1415926 * (angle) / 180)
int main()
{

    double EL = 72;
    double ESLEFT = 0.01;
    double ESRIGHT = 65;
    double ZSLEFT = 0.01;
    double ZSRIGHT = 50;

    double n = 0.39;
    double ZL = 76.31;
    double fa = 2.5;
    double f0 = 2;
    double fd = 1.25;
    double Z0 = 50.00;
    double ELTmpFa = 0;
    double ESTmpFa = 0;
    double ELTmpFd = 0;
    double ESTmpFd = 0;
    std::complex<double> Aa;
    std::complex<double> Ad;
    std::complex<double> Bd;
    std::complex<double> Cd;
    std::complex<double> Dd;
    std::complex<double> S11d;
    int FinalFlag = 0;
    double FlagDis = 0;
    double FlagMatch = 0;
    double FineES = 0;
    double FineZS = 0;
    double FineESInDis = 0;
    double FineZSInDis = 0;
    double FineESInMatch = 0;
    double FineZSInMatch = 0;
    double FineESInTry = 0;
    double FineZSInTry = 0;
    double LargestDistance = 99999;
    double LargestDistanceTry = 99999;
    double LargestDistanceInDis = 99999;
    double LargestDistanceInMatch = 99999;
    for (double ES = ESLEFT; ES <= ESRIGHT; ES += 0.01)
    {
        ELTmpFa = A2P(EL) * fa / f0;
        ESTmpFa = A2P(ES) * fa / f0;
        ELTmpFd = A2P(EL) * fd / f0;
        ESTmpFd = A2P(ES) * fd / f0;
        for (double ZS = ZSLEFT; ZS <= ZSRIGHT; ZS += 0.01)
        {
            Aa = cos(ESTmpFa) * cos(ESTmpFa) + (sin(ESTmpFa) * (ZL * cos(ESTmpFa) * tan(ELTmpFa) * n * n * J + ZS * sin(ESTmpFa) * J) * J) / ZS;
            Ad = cos(ESTmpFd) * cos(ESTmpFd) + (sin(ESTmpFd) * (ZL * cos(ESTmpFd) * tan(ELTmpFd) * n * n * J + ZS * sin(ESTmpFd) * J) * J) / ZS;
            Bd = cos(ESTmpFd) * (ZL * cos(ESTmpFd) * tan(ELTmpFd) * n * n * J + ZS * sin(ESTmpFd) * J) + ZS * cos(ESTmpFd) * sin(ESTmpFd) * J;
            Cd = (cos(ESTmpFd) * sin(ESTmpFd) * J) / ZS + (sin(ESTmpFd) * (cos(ESTmpFd) - (ZL * n * n * sin(ESTmpFd) * tan(ELTmpFd)) / ZS) * J) / ZS;
            Dd = cos(ESTmpFd) * (cos(ESTmpFd) - (ZL * n * n * sin(ESTmpFd) * tan(ELTmpFd)) / ZS) - sin(ESTmpFd) * sin(ESTmpFd);
            S11d = (Ad * Z0 + Bd - Cd * Z0 * Z0 - Dd * Z0) / (Ad * Z0 + Bd + Cd * Z0 * Z0 + Dd * Z0);
            // (abs(1.0 + Aa) < MAX_ERROR) &&
            FlagDis = abs(1.0 + Aa);
            FlagMatch = abs(S11d);
            if ((FlagDis < MAX_ERROR) && (FlagMatch < MAX_ERROR))
            {
                FinalFlag = 1;
                // 综合距离最小的解
                if (FlagDis + FlagMatch < LargestDistance)
                {
                    LargestDistance = FlagDis + FlagMatch;
                    FineES = ES;
                    FineZS = ZS;
                }
                // 色散最好解
                if (FlagDis < LargestDistanceInDis)
                {
                    LargestDistanceInDis = FlagDis;
                    FineESInDis = ES;
                    FineZSInDis = ZS;
                }
                // 匹配最好解
                if (FlagMatch < LargestDistanceInMatch)
                {
                    LargestDistanceInMatch = FlagMatch;
                    FineESInMatch = ES;
                    FineZSInMatch = ZS;
                }
            }
            else
            {
                if (FlagDis + FlagMatch < LargestDistanceTry)
                {
                    FineESInTry = ES;
                    FineZSInTry = ZS;
                    LargestDistanceTry = FlagDis + FlagMatch;
                }
            }
        }
    }
    if (FinalFlag != 0)
    {
        printf("综合最好解: ES = %lf, ZS = %lf 差距: %lf\n", FineES, FineZS, LargestDistance);
        printf("色散最好解: ES = %lf, ZS = %lf 差距: %lf\n", FineESInDis, FineZSInDis, LargestDistanceInDis);
        printf("匹配最好解: ES = %lf, ZS = %lf 差距: %lf\n", FineESInMatch, FineZSInMatch, LargestDistanceInMatch);
    }
    else
    {
        printf("最收敛尝试: ES = %lf, ZS = %lf 差距: %lf\n", FineESInTry, FineZSInTry, LargestDistanceTry);
    }
    return 0;
}
*/